import Vue from 'vue'
export default {
  props: {
    type: {}
  },
  data () {
    return {
      busiName: 'CardVoucher',
      drawerVisible: false,
      paramsForm: {},
      submitLoading: false,
      selectRow: {},
      thumbImage: [],
      dialogVisible: false,
      storeOpts: [],
    }
  },
  async created () {
    this.nodetypeOpts = await this.$store.dispatch('options/getOptions', { busiName: this.busiName, rela: 101 })
    this.storeOpts = await this.$store.dispatch('options/getStoreOptions')
  },
  methods: {
    addCoupon (v) {
      this.paramsForm = {
        类型: this.type,
        门店: [],
        主图: []
      }
      this.thumbImage = []
      this.drawerVisible = true
      this.selectRow = {}
    },
    saveCoupon (data) {
      return new Promise((resolve, reject) => {
        let api = `/api/Insert/${this.busiName}`
        let params = {}
        if (this.selectRow.id) {
          api = `/api/UpdateById/${this.busiName}`
          params.rowId = this.selectRow.id
        }
        this.$command.execute({
          api,
          params,
          data,
          hideMsg: true
        }).then(res => {
          return resolve(true)
        }).catch(err => {
          return resolve(false)
        })
      })
    },
    changeInput (e, key) {
      if (!e) return ''
      if (Number(e) <= 0) {
        e = '0'
      }
      this.paramsForm[key] = Vue.filter('pointNum')(e)
    },
    changeNumber (e, key) {
      if (!e) return ''
      if (Number(e) <= 0) {
        e = 0
      }
      if (Number(e) > 100) {
        e = 100
      }
      this.paramsForm[key] = e
    },
    changeTime (e, key) {
      console.log(!e)
      if (!e) {
        this.paramsForm[key] = ''
        return
      }
      if (Number(e) <= 0) {
        e = 0
      }
      this.paramsForm[key] = e
    },
    //删除
    async deleteRow (row) {
      //查询是否可以删除
      let canDeleted = await this.$store.dispatch('app/canDeleteRow', { busiName: `${this.busiName}.${this.type}`, rowId: row.id })
      if (!canDeleted) return
      this.$command.execute({
        api: `/api/DelRowById/${this.busiName}`,
        params: { rowId: row.id }
      }).then(res => {
        this.$message.success('删除成功')
        this.refreshList()
      })
    }
  }
}
